package com.stupro.cjlu.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stupro.cjlu.common.utils.VOToEntity;
import com.stupro.cjlu.common.vo.ResultVO;
import com.stupro.cjlu.common.vo.TeacherCourseItemVO;
import com.stupro.cjlu.common.vo.TeacherInsertVO;
import com.stupro.cjlu.config.themis.annotation.Student;
import com.stupro.cjlu.dao.CourseDAO;
import com.stupro.cjlu.dao.StudentCourseDAO;
import com.stupro.cjlu.dao.TeacherDAO;
import com.stupro.cjlu.entity.Studentcourse;
import com.stupro.cjlu.entity.Teacher;
import com.stupro.cjlu.manager.TeacherManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2021-05-10
 */
@Service
public class TeacherService extends BaseService{

    @Autowired
    private TeacherDAO teacherDAO;

    private final TeacherManager manager;

    @Autowired
    private CourseDAO courseDAO;

    @Autowired
    private StudentCourseDAO studentCourseDAO;

    public TeacherService(TeacherManager manager) {
        this.manager = manager;
    }

    public ResultVO getTeacher(Integer page,String name){
        IPage data = teacherDAO.getByPage(page,name);
        List<Teacher> list = data.getRecords();
        //隐藏密码
        for (int i = 0; i < list.size(); i++) {
            Teacher temp = list.get(i);
            temp.setTeaPwd(null);
            list.set(i,temp);
        }
        ResultVO result = result(list);
        result.setTotal(data.getTotal());
        return result;
    }


    public ResultVO getCourse(Integer page, String teacherName, String className) {

        IPage<TeacherCourseItemVO> data = teacherDAO.getCourserByPage(page,teacherName,className);
        ResultVO result = result(data);
        result.setTotal(data.getTotal());
        return result;
    }

    public ResultVO delById(int id) {
        int count = courseDAO.getCourseCountByTeacherId(id);
        if(count==0){
            return result(teacherDAO.delById(id));
        }
        else {
            return result("改老师还有课程在进行");
        }

    }

    public ResultVO update(Teacher teacher) {
        Teacher temp = teacherDAO.get(teacher.getTeaID());
        if(temp!=null){
            return result(teacherDAO.update(teacher));
        }else{
            return result("失败");
        }
    }

    public ResultVO getCourseById(int teaID) {
        String teaName = teacherDAO.get(teaID).getTeaName();
        ResultVO resultVO = result(teacherDAO.getCourseByTeacherId(teaID));
        return resultVO;
    }

    public ResultVO changePwd(TeacherInsertVO teacherVO) {
        Teacher teacher = VOToEntity.toTeacher(teacherVO);
        Boolean flag = teacherDAO.getByTeacher(teacher);
        if(flag){
            Teacher t = new Teacher();
            t.setTeaID(teacher.getTeaID());
            t.setTeaPwd(teacherVO.getNewPwd());
            return result(teacherDAO.update(t));
        }else{
            return result("密码错误");
        }
    }

    public ResultVO checkPwd(int teacherId,String pwd) {
        Boolean result = teacherDAO.checkPwd(teacherId,pwd);
        return result(result);
    }

    public ResultVO create(Teacher entity) {
        if (manager.get(entity.getTeaID()) != null) {
            return failedResult("教师Id: " + entity.getTeaID() + "已存在!");
        }
        //     entity.setPassword(userService.computePasswordHash(entity.getPassword()));
        manager.create(entity);
        return result("添加成功");
    }

    public ResultVO get(Integer id) {
        Teacher entity = manager.get(id);
        if (entity == null) {
            return failedResult("教师Id: " + id + "不存在!");
        }

        entity.setTeaPwd("");

        return result(entity);
    }

    public ResultVO getPageCount(String name) {
        return result(manager.getPageCount(name));
    }

    public ResultVO changeGrade(int scID, int grade) {
        Studentcourse studentcourse = new Studentcourse(scID,null,null,new BigDecimal(grade));
        return result(studentCourseDAO.update(studentcourse));
    }
}
